package com.hfzy.ihk.web.linkcall.job.common;

import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.common.util.http.HttpClientUtils;
import com.hfzy.ihk.web.linkcall.constant.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

/**
 * 重推通知AI服务知识库数据的job
 * 通过加载缓存中推送失败的id，进行重推操作
 * */
//@Component
@Deprecated
public class ReAdviceLoadKnowledgeBaseDataJob {
    private static final Logger logger = LoggerFactory.getLogger(ReAdviceLoadKnowledgeBaseDataJob.class);

    @Autowired
    RedisTemplate redisTemplate;

    @Value("${aiService.knowledgebase.analysis.singleCall.url}")
    public String singleCallUrl;

    /**
     * 每10min 执行一次
     * */
//    @Scheduled(cron = "0 0/10 * * * ?")
    public void reAdviceIds(){
        logger.info("执行job通知ai服务重新加载指定知识库数据 start "+Utils.date2DateTimeString(new Date()));
        Set<String> ids = redisTemplate.opsForSet().members(RedisKey.RE_ADVICE_LOAD_KNOWLEDGE_BASE_IDS);
        for(String id:ids){
            try{
                String result = HttpClientUtils.getMethod(singleCallUrl + "/knowledgebase/" + id);
                if(!Utils.isNullString(result) && "true".equals(result)){//解析接受到的结果集，如果判断为异常情况下，再次进行重推操作
                    redisTemplate.opsForSet().remove(RedisKey.RE_ADVICE_LOAD_KNOWLEDGE_BASE_IDS,id);
                }
            }catch (Exception e){
                logger.error("通知ai服务更新知识库数据失败，id：｛｝",id,e);
                e.printStackTrace();
            }
        }
        logger.info("执行job通知ai服务重新加载指定知识库数据 end "+Utils.date2DateTimeString(new Date()));
    }
}
